package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.Console;
import java.io.InputStreamReader;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
 * Created by abhishekpuri on 10/5/14.
 */
public class IOHandler {

    private BufferedReader inStream;

    public IOHandler() {
        this.inStream =new BufferedReader(new InputStreamReader(System.in));
    }

    public IOHandler(BufferedReader input){
        this.inStream =input;
    }
    public String readString() {
        try {
            return inStream.readLine();
        }
        catch (Exception ex) {
            printToErrorLog("BibAppErr : " + ex.getMessage());
            return null;
        }
    }

    public Integer readInt()  {
        try {
             return Integer.parseInt(this.readString());
        }
        catch (Exception ex) {
            printToErrorLog("BibAppErr : " + ex.getMessage());
            return null;
        }
    }

    public Date readDate()
    {
        try {
            SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
            return dateFormatter.parse(this.readString());
        }
        catch (Exception ex)
        {
            printToErrorLog("BibAppErr : " + ex.getMessage());
            return null;
        }
    }

    public void printlnToConsole(String output) {
        System.out.println(output);
    }

    public void printToConsole(String output) {
        System.out.print(output);
    }

    public void printToErrorLog(String err) {
        System.err.println(err);
    }
}
